package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodDao;
import com.itheima.dao.MedicineDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Food;
import com.itheima.pojo.Medicine;
import com.itheima.service.FoodService;
import com.itheima.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MedicineService.class)
@Transactional
public class MedicineServiceImpl implements MedicineService {

    @Autowired
    private MedicineDao medicineDao;

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用分页插件进行分页
        PageHelper.startPage(currentPage,pageSize);
        //调用方法进行查询操作
        Page page = medicineDao.selectByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }


    //添加数据
    @Override
    public void add(List<Medicine> medicines) {
        if (medicines != null && medicines.size() >0){
            for (Medicine medicine : medicines) {
                medicineDao.add(medicine);
            }
        }
    }
}
